package service

import (
	sqlc "bgs/db/sqlc"
	"bgs/util"
	"context"
	"database/sql"
)

// EnsureStoreStat 确保StoreStat对于一个门店始终有一条记录
func (s *StoreServiceImp) EnsureStoreStatPtr(ctx context.Context, q *sqlc.Queries, withLock bool, storeID int64) (storeStatPtr *sqlc.StoreStat, err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	var sqlErr error
	var storeStat sqlc.StoreStat
	if withLock {
		storeStat, sqlErr = q.GetStoreStatByStoreIDToLock(ctx, storeID)
	} else {
		storeStat, sqlErr = q.GetStoreStatByStoreID(ctx, storeID)
	}

	if sqlErr != nil {
		if sql.ErrNoRows != sqlErr {
			err = sqlErr
			return
		}
		slog.Infof("找不到品牌统计记录")
		storeStat, err = q.UpsertStoreStat(ctx, storeID)
		if err != nil {
			return
		}
	}
	storeStatPtr = &storeStat
	slog.Infof("确保StoreStat对于一个门店始终有一条记录并返回 ok")
	return
}


// UpdateStoreStatFansCountSinceUpsertStoreFan (主动,门店统计更新粉丝数量)
func (s *StoreServiceImp) UpdateStoreStatFansCountSinceUpsertStoreFan(ctx context.Context, q *sqlc.Queries, storeID int64) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	brandStatLockedPtr, err := s.EnsureStoreStatPtr(ctx, q, true, storeID)
	if err != nil {
		return
	}
	slog.Infof("1、store_fans-确保获取锁定的品牌(%d)统计记录 ok", storeID)
	slog.Infof("2、store_fans 更新前fans_count(%d) ok", brandStatLockedPtr.FansCount)

	fansCount, err := q.AggrStoreFansCountByStoreID(ctx, storeID)
	if err != nil {
		return
	}
	slog.Infof("3、统计当前门店的fans_count ok")

	err = q.UpdateStoreStatFansCountByStoreID(ctx, sqlc.UpdateStoreStatFansCountByStoreIDParams{
		StoreID:   storeID,
		FansCount: int32(fansCount),
	})
	if err != nil {
		return
	}
	slog.Infof("4、store_fans 更新前fans_count(%d) ok", fansCount)

	slog.Infof("=门店统计更新粉丝数量 ok")

	return
}


// UpdateStoreStatSpokesmanCountSinceUpsertStoreSpokesman (主动,门店统计更新代言人B数量)
func (s *StoreServiceImp) UpdateStoreStatSpokesmanCountSinceUpsertStoreSpokesman(ctx context.Context, q *sqlc.Queries, storeID int64) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	storeStatLockedPtr, err := s.EnsureStoreStatPtr(ctx, q, true, storeID)
	if err != nil {
		return
	}
	slog.Infof("1、spokesmans-确保获取锁定的门店(%d)统计记录 ok", storeID)
	slog.Infof("2、spokesmans 更新前spokesman_count(%d) ok", storeStatLockedPtr.SpokesmanCount)

	spokesmanCount, err := q.AggrStoreSpokesmanCountByStoreID(ctx, sqlc.NInt64FromInt64(storeID))
	if err != nil {
		return
	}
	slog.Infof("3、统计当前门店的spokesman_count ok")

	err = q.UpdateStoreStatSpokesmanCountByStoreID(ctx, sqlc.UpdateStoreStatSpokesmanCountByStoreIDParams{
		StoreID:        storeID,
		SpokesmanCount: int32(spokesmanCount),
	})
	if err != nil {
		return
	}
	slog.Infof("4、spokesmans 更新后spokesman_count(%d) ok", spokesmanCount)

	slog.Infof("=门店统计更新代言人B数量 ok")

	return
}


// UpdateStoreStatEndorseRequestCountSinceCreateStoreEndorsePendingRequest (主动,门店统计更新未审核的代言人申请数量)
func (s *StoreServiceImp) UpdateStoreStatEndorseRequestCountSinceCreateStoreEndorsePendingRequest(ctx context.Context, q *sqlc.Queries, storeID int64) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	storeStatLockedPtr, err := s.EnsureStoreStatPtr(ctx, q, true, storeID)
	if err != nil {
		return
	}
	slog.Infof("1、store_endorse_request-确保获取锁定的门店(%d)统计记录 ok", storeID)
	slog.Infof("2、store_endorse_request 更新前pending_endorse_request_count(%d) ok", storeStatLockedPtr.EndorseRequestToAuditCount)

	endorseRequestToAuditCount, err := q.AggrStoreEndorseRequestPendingCountByStoreID(ctx, storeID)
	if err != nil {
		return
	}
	slog.Infof("3、统计当前门店的pending_endorse_request_count ok")

	err = q.UpdateStoreStatSpokesmanCountByStoreID(ctx, sqlc.UpdateStoreStatSpokesmanCountByStoreIDParams{
		StoreID:        storeID,
		SpokesmanCount: int32(endorseRequestToAuditCount),
	})
	if err != nil {
		return
	}
	slog.Infof("4、store_endorse_request 更新后pending_endorse_request_count(%d) ok", endorseRequestToAuditCount)

	slog.Infof("=门店统计更新代言人B数量 ok")

	return
}


// UpdateStoreStatPublishActivityCountSinceStoreOwnActivityPublished (主动,门店统计自身发布的活动数量)
func (s *StoreServiceImp) UpdateStoreStatPublishActivityCountSinceStoreOwnActivityPublished(ctx context.Context, q *sqlc.Queries, storeID int64, asFeatureTypes []string) (err error) {
	slog := util.NewContextLogger(ctx, s.slog)

	brandStatLockedPtr, err := s.EnsureStoreStatPtr(ctx, q, true, storeID)
	if err != nil {
		return
	}
	slog.Infof("1、activities-确保获取锁定的门店(%d)统计记录 ok", storeID)
	slog.Infof("2、activities 更新前publish_activity_count(%d) ok", brandStatLockedPtr.PublishActivityCount)

	publishActivityCount, err := q.AggrActivityCountPublishedOfStoreOwnByStoreID(ctx, sqlc.AggrActivityCountPublishedOfStoreOwnByStoreIDParams{
		BrandID:        sqlc.NInt64FromInt64(storeID),
		AsFeatureTypes: asFeatureTypes,
	})
	if err != nil {
		return
	}
	slog.Infof("3、统计当前门店的publishActivityCount ok")

	err = q.UpdateStoreStatPublishActivityCountByStoreID(ctx, sqlc.UpdateStoreStatPublishActivityCountByStoreIDParams{
		StoreID:              storeID,
		PublishActivityCount: int32(publishActivityCount),
	})
	if err != nil {
		return
	}
	slog.Infof("4、activities 更新前publish_activity_count(%d) ok", publishActivityCount)

	slog.Infof("=门店统计自身发布的活动数量 ok")

	return
}